import java.awt.Color;

import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;

public class Cell implements Drawable {
	
	// enums
	public static enum Density { none, low, medium, high }
    public static enum State { normal, burning, burnt, out, cut } 

    // constants
	private static final int extinctTicks = 3;
	private static final Color burntColor = Color.DARK_GRAY;
	private static final Color onFireColor = Color.RED;	
	private static final Color outColor = new Color(100, 200, 255);    
    private static final Color cutColor = new Color(120, 40, 0);
    private static final int maxCutTicks = 3;    

	// variables
    private Point p;
    private Color color;
    private State state;
    private Density density;
    private int fireTicks;
    private int waterTicks;    
    private boolean hasFirefighter = false;
    private int cutTicks;

    public Cell(Point p, Density density, State state) {        
    	this.p = p;
        this.density = density;
        this.state = state;
        this.fireTicks = 0;
        this.waterTicks = 0;
        this.cutTicks = 0;

        switch (this.density) {
        
        case none:
            this.color = RFSModel.mapColors.get(0);
            break;
            
        case low:
            this.color = RFSModel.mapColors.get(2);
            break;
            
        case medium:        	
            this.color = RFSModel.mapColors.get(3);
            break;
            
        case high:        	
            this.color = RFSModel.mapColors.get(4);
            break;
    
        default:
        	break;
    	}
    }
    
    public int getCutTicks() {
    	return cutTicks;
    }
    
    public void increaseCutTicks() {
    	cutTicks++;
    }
    
    public boolean isCut() {
    	return cutTicks >= maxCutTicks;
    }
         
    @Override
    public void draw(SimGraphics g) {
        g.drawFastCircle(color);
    }
    
    public Color getColor() {
        return color;
    }
    
    public Density getDensity() {
        return density;
    }
    
    public int getFireTicks() {
		return fireTicks;
	}
    
    public Point getPoint() {
    	return p;  
    }
    
    public State getState() {
        return state;
    }
           
    public int getWaterTicks() {
		return waterTicks;
	}

    @Override
	public int getX() {
		return p.x;
	}

    @Override
	public int getY() {
		return p.y;
	}

    public boolean hasFirefighter() {
    	return hasFirefighter;
    }
    
    public void increaseFireTicks() {
		fireTicks++;
	}
    
    public void increaseWaterTicks() {
		waterTicks++;
    	if (waterTicks == extinctTicks)
			setOut(); 
    }
    
    public boolean isBurning() {
    	return state == State.burning;
    }
    
    public boolean isBurnt() {
    	return state == State.burnt;
    }

    public boolean isHigh() {
    	return density == Density.high;
    }

    public boolean isLow() {
    	return density == Density.low;
    }
    
    public boolean isMedium() {
    	return density == Density.medium;
    }
    
    public boolean isNone() {
    	return density == Density.none;
    }
    

    public boolean isNormal() {
    	return state == State.normal;
    }

	public boolean isOut() {
    	return state.equals(State.out);
    }

	public void setBurnt() {
        this.color = burntColor;
        this.state = State.burnt;
    }
		
	public void setCut() {
    	color = cutColor;
    	state = State.cut;
    }
	
	public void setHasFirefighter(boolean b) {
    	hasFirefighter = b;
    }
	
	public void setOnFire() {
        this.color = onFireColor;
        this.state = State.burning;
        this.fireTicks++;
    }
	
	public void setOut() {
        this.color = outColor;
        this.state = State.out;
    }
	
	public String toString() {
		String ret = "";
		ret += this.fireTicks + " fireticks\n";
		ret += this.hasFirefighter + " hasfirefigher\n";
		ret += this.waterTicks + " waterticks\n";
		ret += this.state + " state\n";
		return ret;
	}

	public void setColorBurning() {
		int r = 255 - 2*fireTicks;
		if (r < 0)
			r = 0;
		this.color = new Color (r,0,0);
	}
	
}
